var own = {
    /**
     * 配置文件路径
     */
    configPath: '/js/own/own.config.js',
    /**
     * 记录当前是否存在请求的ajax
     */
    nowAjax: false,
    /**
     * 获取项目跟目录
     * @returns {string}
     */
    _getRootPath_: function () {
        //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
        var curWwwPath = window.document.location.href;
        //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
        var pathName = window.document.location.pathname;
        var pos = curWwwPath.indexOf(pathName);
        //获取主机地址，如： http://localhost:8083
        var localhostPaht = curWwwPath.substring(0, pos) + "/";
        return (localhostPaht);
    },
    /**
     * 初始化加载
     */
    init: function () {
        own.addLoad();
        /**
         * 当前own.js所在文件夹
         */
        var nowPath = own._getRootPath();

        var configPath = nowPath + "own.config.js";
        /**
         * 当前项目跟路径，两种情况，存在项目名与不存在项目名
         * @type {string}
         */
        var localhostPath = "";

        //判断浏览器版本
        own._load(own._getRootPath_() + 'my-config.js', function () {


            own._load(configPath, function () {
                //获取第一条配置信息，根据第一条配置信息截获出项目名对应信息

                var utilPath = own.config.modules.util.substring(1);
                var root = utilPath.substring(0, utilPath.indexOf('/'));
                localhostPath = nowPath.substring(0, nowPath.indexOf('/' + root + '/'));

                own.config.localhostPath = localhostPath;

                // own._load(localhostPath + own.config.modules.owncss);
                // own._load(localhostPath + own.config.modules.velocity);
                // own._load(localhostPath + own.config.modules.event);
                own._load(own.config.modules.velocity, function () {
                    own._load(own.config.modules.ajaxhook, function () {
                        window.top.isLogin = 0;
                        hookAjax({
                            //拦截回调
                            onreadystatechange: function (xhr) {
                                var url = (xhr.responseURL == undefined || xhr.responseURL == null) ? '' : xhr.responseURL;
                                // console.log(xhr);
                                console.log(window.top.isLogin + "==" + (window.top.isLogin != 1 && (xhr.status == 510 || xhr.status == 511)));
                                if (window.top.isLogin != 1 && (xhr.status == 510 || xhr.status == 511)) {
                                    window.top.isLogin = 1;
                                    layer.closeAll();
                                    if (xhr.status == 510) {//无访问权限或者登录失效
                                        window.top.njzxw.alert({
                                            error: true,
                                            msg: '您未登录或登录已失效，请重新登录。',
                                            fun: function () {
                                                window.top.isLogin = 0;
                                                window.top.location.href = own.config.localhostPath + '/login.html';
                                            }
                                        });
                                    } else if (xhr.status == 511) {
                                        njzxw.alert({
                                            msg: '您访问未授权，请联系管理员添加访问权限。',
                                            error: true,
                                            fun: function () {
                                                window.top.isLogin = 0;
                                            }
                                        });
                                    }
                                }

                                if (xhr.readyState == 4 && url != '' && url.indexOf('.js') < 0 && url.indexOf('.css') < 0) {
                                    try {
                                        own.hideLoad();
                                    } catch (e) {
                                    }
                                    own.nowAjax = false;
                                }
                            },
                            onload: function (xhr) {
                                var url = (xhr.responseURL == undefined || xhr.responseURL == null) ? '' : xhr.responseURL;
                                if (url != '' && url.indexOf('.js') < 0 && url.indexOf('.css') < 0) {
                                    try {
                                        own.hideLoad();
                                    } catch (e) {
                                    }
                                    own.nowAjax = false;
                                }
                            },
                            //拦截函数
                            open: function (arg) {
                                if (arg[1].indexOf('.js') < 0 && arg[1].indexOf('.css') < 0) {
                                    if (own.nowAjax) {
                                        if (arg[1].indexOf('update') >= 0 || arg[1].indexOf('delete') >= 0 || arg[1].indexOf('insert') >= 0) {
                                            layui.layer.msg('请不要重复操作哦', {icon: 5});
                                        }
                                    } else {
                                        own.nowAjax = true;
                                        own.addLoad();
                                    }
                                }
                                // console.log("open called: method:%s,url:%s,async:%s",arg[0],arg[1],arg[2])
                            }
                        })

                        own._load(own.config.modules.util, function () {
                            own._load(own.config.modules.request, function () {
                                document.onload = document.onreadystatechange = function () {
                                    if (document.readyState == "complete") {
                                        var objs = own._getScriptObjct();
                                        //如果全局只引入一个js,就拦截直接获取
                                        var src_ = objs.src;
                                        var srccnt = src_.lastIndexOf('?');
                                        var moduleName = '';
                                        if (srccnt != -1) {
                                            moduleName = src_.substr(srccnt + 1);
                                        }
                                        $$.request("browser", function () {
                                            var version = own.browser.getVersion();
                                            if (version == own.browser.version.IE8 || version == own.browser.version.IE9) {
                                                $$.request("html5,respond");
                                            }
                                            if (srccnt == -1) {
                                                own.hideLoad();
                                                var script = document.createElement('script');
                                                script.innerHTML = objs.innerHTML;
                                                document.head.appendChild(script);
                                            }
                                        });
                                        if (srccnt != -1 && $$.util.trim(moduleName) != '') {
                                            moduleName = moduleName.replace('layui', '');
                                            $$.request(moduleName, function () {
                                                own._load(localhostPath + own.config.modules.layui, function () {
                                                    own.hideLoad();
                                                    // eval(objs.innerText);
                                                    var script = document.createElement('script');
                                                    script.innerHTML = objs.innerHTML;
                                                    document.head.appendChild(script);
                                                });

                                            });
                                        }
                                    }
                                }
                            }, own.config.localhostPath);
                        }, own.config.localhostPath);
                    }, own.config.localhostPath);
                }, own.config.localhostPath);
            });
        });
        own.hideLoad();
    },
    /**
     * 获取当前own.js运行目录对应的文件夹
     * @returns {*}
     * @private
     */
    _getRootPath: function () {
        var src = null;
        try {
            array[1];
        } catch (e) {
            var stack = e.stack || e.sourceURL || e.stacktrace || '';
            var rgx = /(?:http|https|file):\/\/.*?\/.+?\.js/;
            src = (rgx.exec(stack) || [])[0] || '';
            if (src != null) {
                src = src.substring(0, src.lastIndexOf('/') + 1);
            }
        }
        if (src == null) {
            var doc = document;
            var jsPath = doc.currentScript ? doc.currentScript.src : function () {
                var js = doc.scripts
                    , last = js.length - 1
                    , src;
                for (var i = last; i > 0; i--) {
                    if (js[i].readyState === 'interactive') {
                        src = js[i].src;
                        break;
                    }
                }
                return src || js[last].src;
            }();
            src = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
        }
        return src;
    },
    /**
     * 加载js
     * @param url
     * @param fun
     */
    _load: function (url, fun, path) {
        if (url.lastIndexOf('.js') != -1) {
            var obj = document.createElement('script');
            obj.type = 'text/javascript';
            if (path) {
                url = path + url;
            }
            obj.id = url;
            obj.charset = 'UTF-8';
            obj.src = url;
            obj.onload = obj.onreadystatechange = function () {
                if (!obj.readyState || obj.readyState === "loaded" || obj.readyState === "complete") {
                    if (typeof (fun) == 'function') {
                        fun();
                    }
                }
            }
            document.head.appendChild(obj);
        } else {
            var obj = document.createElement('link');
            obj.rel = 'stylesheet';
            if (typeof (own.util) == 'object') {
                obj.id = own.util.hashCode(url);
            } else {
                obj.id = url;
            }
            obj.charset = 'UTF-8';
            obj.href = url;
            document.head.appendChild(obj);
        }

    },
    _getScriptObjct: function () {
        var scriptSrc = document.getElementsByTagName('script');
        for (var i = 0, cnt = scriptSrc.length; i < cnt; i++) {
            if (scriptSrc[i].src.indexOf('own.js') >= 0) {
                return scriptSrc[i];
            }
        }
    },
    addLoad: function () {
        if (own.hideLoadObj != null) {
            window.clearTimeout(own.hideLoadObj);
            return;
        }

        var zxwBgLoading = document.querySelector('#layui-layer-shade_zxw');
        if (zxwBgLoading == null) {
            var html = '<div class="layui-layer-shade" id="layui-layer-shade_zxw" times="1" style="z-index: 19891014; background-color: rgb(0, 0, 0); opacity: 0.5;"></div>' +
                '<div class="layui-layer layui-layer-loading" id="layui-layer_zxw" type="loading" times="1" showtime="0" contype="string" style="z-index: 19891015; width: 360px; top: 50%; margin-top: -16px; left: 50%; margin-left: -180px"><div id="" class="layui-layer-content layui-layer-loading2"></div><span class="layui-layer-setwin"></span></div>';
            document.querySelector('body').insertAdjacentHTML('afterbegin', html);
        }
    },
    hideLoadObj: null,
    hideLoad: function () {
        if (own.hideLoadObj != null) {
            window.clearTimeout(own.hideLoadObj);
            own.hideLoadObj = null;
        }
        //延迟500毫秒消失
        own.hideLoadObj = window.setTimeout(function () {
            var zxwBgLoading = document.querySelector('#layui-layer-shade_zxw');
            var zxwIcoLoading = document.querySelector('#layui-layer_zxw');
            if (zxwBgLoading != undefined) {
                if (typeof (Velocity) != 'undefined') {
                    Velocity(zxwBgLoading, {
                        opacity: 0
                    }, {
                        duration: 1000,
                        easing: "ease-in-out",
                        complete: function () {
                            try {
                                own.hideLoadObj = null;
                                document.body.removeChild(zxwBgLoading);
                            } catch (e) {
                            }
                        }
                    });
                }
            }
            if (zxwIcoLoading != undefined) {
                if (typeof (Velocity) != 'undefined') {
                    Velocity(zxwIcoLoading, {
                        opacity: 0
                    }, {
                        duration: 1000,
                        easing: "ease-in-out",
                        complete: function () {
                            try {
                                // alert(2);
                                own.hideLoadObj = null;
                                document.body.removeChild(zxwIcoLoading);
                            } catch (e) {
                            }
                        }
                    });
                }
            }

        }, 500);
    }
};

window.$own = $$ = own;
//执行初始化操作
own.init();